package cn.com.acca.ma.dao.impl;

import cn.com.acca.ma.dao.ConceptBoardIndexDao;
import cn.com.acca.ma.hibernate.util.HibernateUtil;
import cn.com.acca.ma.model.ConceptBoardIndex;
import org.hibernate.SQLQuery;

import java.util.List;

public class ConceptBoardIndexDaoImpl extends BaseDaoImpl<ConceptBoardIndexDaoImpl> implements ConceptBoardIndexDao {

    /**
     * 插入ConceptBoardIndex记录
     * @param conceptBoardIndex
     */
    @Override
    public void save(ConceptBoardIndex conceptBoardIndex) {
        session= HibernateUtil.currentSession();
        session.beginTransaction();
        session.save(conceptBoardIndex);
        session.getTransaction().commit();
        session.close();
    }

    /**
     * 插入ConceptBoardIndex记录列表
     * @param conceptBoardIndexList
     */
    @Override
    public void batchSave(List<ConceptBoardIndex> conceptBoardIndexList) {
        session= HibernateUtil.currentSession();
        session.beginTransaction();
        for (ConceptBoardIndex conceptBoardIndex: conceptBoardIndexList){
            session.save(conceptBoardIndex);
        }
        session.getTransaction().commit();
        session.close();
    }

    /**
     * truncate表
     */
    @Override
    public void truncateTable() {
        session= HibernateUtil.currentSession();
        session.beginTransaction();
        session.createSQLQuery("truncate table concept_board_index").executeUpdate();
        session.getTransaction().commit();
        session.close();
    }

    /**
     * 海量计算last_close_price、change_range和change_amount字段
     */
    @Override
    public void writeConceptBoardIndexLastClosePrice() {
        session= HibernateUtil.currentSession();
        session.beginTransaction();

        SQLQuery query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_LAST_CLOSE_PRICE()}");
        query.executeUpdate();

        session.getTransaction().commit();
        session.close();
    }

    /**
     * 海量计算KD指标
     */
    @Override
    public void writeConceptBoardIndexKD() {
        session= HibernateUtil.currentSession();
        session.beginTransaction();

        SQLQuery query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_INIT()}");
        query.executeUpdate();

        query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_RSV()}");
        query.executeUpdate();

        query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_K()}");
        query.executeUpdate();

        query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_D()}");
        query.executeUpdate();

        session.getTransaction().commit();
        session.close();
    }

    /**
     * 增量计算last_close_price、change_range和change_amount字段
     * @param beginDate
     * @param endDate
     */
    @Override
    public void writeConceptBoardIndexLastClosePriceByDate(String beginDate, String endDate) {
        session= HibernateUtil.currentSession();
        session.beginTransaction();

        SQLQuery query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_LAST_CLOSE_PRICE_BY_DATE(?,?)}");
        query.setParameter(0, beginDate);
        query.setParameter(1, endDate);
        query.executeUpdate();

        session.getTransaction().commit();
        session.close();
    }

    /**
     * 增量计算KD指标
     */
    @Override
    public void writeConceptBoardIndexKDByDate(String date) {
        session= HibernateUtil.currentSession();
        session.beginTransaction();

        SQLQuery query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_BY_DATE_RSV(?)}");
        query.setParameter(0, date);
        query.executeUpdate();

        query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_BY_DATE_K(?)}");
        query.setParameter(0, date);
        query.executeUpdate();

        query = session
                .createSQLQuery("{call PKG_CONCEPT_BOARD.WRITE_KD_BY_DATE_D(?)}");
        query.setParameter(0, date);
        query.executeUpdate();

        session.getTransaction().commit();
        session.close();
    }
}
